package com.example.testapp.activity.activity.read;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.testapp.R;

/**
 * Created by l on 2016/7/31.
 */
public class ScrollHideListActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private ListView mListView;
    private String[] mStr = new String[20];
    private int mTouchSlop;
    private float  mFirstY;
    private float  mCurrentY;
    private int  direction;
    private ObjectAnimator mAnimator;
    private boolean mShow = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_scroll_hide_list);
        mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
        mToolbar = (Toolbar)findViewById(R.id.toolbar);
        mListView = (ListView)findViewById(R.id.listview);
        for (int i = 0; i < mStr.length; i++){
             mStr[i] = "Item" + i;
        }
        View header = new View(this);
        header.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.activity_horizontal_margin)));
        mListView.addHeaderView(header);
        mListView.setAdapter(new ArrayAdapter<String>(ScrollHideListActivity.this, android.R.layout.simple_expandable_list_item_1, mStr));
        mListView.setOnTouchListener(mTouchListener);
    }

    public View.OnTouchListener mTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    mFirstY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mCurrentY = event.getY();
                    if(mCurrentY - mFirstY > mTouchSlop){
                       direction = 0;
                    }else if(mFirstY - mCurrentY > mTouchSlop){
                       direction = 1;
                    }
                    if(direction == 1){
                       if(mShow){
                           toolbarAnim(1);
                           mShow = !mShow;
                       }
                    }else if(direction == 0){
                        if(mShow){
                            toolbarAnim(0);
                            mShow = !mShow;
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
            return false;
        }
    };

    private void toolbarAnim(int flag) {
         if(mAnimator != null && mAnimator.isRunning()){
             mAnimator.cancel();
         }
        if(flag == 0){
            mAnimator = ObjectAnimator.ofFloat(mToolbar, "translationY", mToolbar.getTranslationY(), 0);
        }else{
            mAnimator = ObjectAnimator.ofFloat(mToolbar, "translationY", mToolbar.getTranslationY(), -mToolbar.getHeight());
        }
        mAnimator.start();
    }
}
